import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/home/HomeView.vue'
import Layout from "@/views/Layout";
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const routes = [


//===================登录==============
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/Login')
    },


//===================主页==============
    {
        path: '/',
        name: 'Layout',
        component: Layout,
        //重定向到首页
        redirect: '/home',
        children: [//子路由
//===================首页==============
            {
                path: 'home',
                name: 'home',
                component: () => import('@/views/home/HomeView'),
            },


            //=====================user====================
            //  会员列表路由
            {
                path: 'userList', name: 'User', component: () => import('@/views/user/User')
            },
            // 会员添加路由
            {
                path: 'addUser', name: 'AddUser', component: () => import('@/views/user/AddUser')
            },
            // 编辑会员路由
            {
                path: 'editUser', name: 'EditUser', component: () => import('@/views/user/EditUser')
            },


            //===================admin=====================
            {
                path: 'adminList', name: 'Admin', component: () => import('@/views/admin/Admin')
            },
            {
                path: 'addAdmin', name: 'AddUser', component: () => import('@/views/admin/AddAdmin')
            },
            {
                path: 'editAdmin', name: 'EditAdmin', component: () => import('@/views/admin/EditAdmin')
            },


            //===================Category=====================
            {
                path: 'CategoryList', name: 'Category', component: () => import('@/views/category/Category')
            },
            {
                path: 'addCategory', name: 'AddCategory', component: () => import('@/views/category/AddCategory')
            },
            {
                path: 'editCategory', name: 'EditCategory', component: () => import('@/views/category/EditCategory')
            },

            //===================Category=====================
            {
                path: 'BookList', name: 'Book', component: () => import('@/views/book/Book')
            },
            {
                path: 'addBook', name: 'AddBook', component: () => import('@/views/book/AddBook')
            },
            {
                path: 'editBook', name: 'EditBook', component: () => import('@/views/book/EditBook')
            },

            //===================Borrow=====================
            {
                path: 'BorrowList', name: 'Borrow', component: () => import('@/views/borrow/Borrow')
            },
            {
                path: 'addBorrow', name: 'AddBorrow', component: () => import('@/views/borrow/AddBorrow')
            },
            {
                path: 'editBorrow', name: 'EditBorrow', component: () => import('@/views/borrow/EditBorrow')
            },


            //===================ReturnBooks=====================
            {
                path: 'ReturnBooksList', name: 'ReturnBooks', component: () => import('@/views/returnBooks/ReturnBooks')
            },

        ]
    },
//===================404==============
    //上面的路由没有时，就会被下面的拦截 404
    {
        path: "*",
        component: () => import('@/views/404')

    }


]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//路由守卫
// 跳转之前  如果路由是'/login'则放行 如果没有admin数据并且路径不是'/login'则强制返回到'/login'
router.beforeEach((to, from, next) => {
    if (to.path === '/login') next()
    const admin = Cookies.get("admin")
    if (!admin && to.path !== '/login') return next("/login")//强制退回到登录页面
    //访问 /home 的时候， 并且cookie里面存在数据，这个时候就直接放行
    next()
})

export default router
